<?php
    class Menu extends Model {

        private $curid = 1;
        private $TYPES;
        const SHOW = 'style="display:block"';
        const HIDE = 'style="display:none"';

        function __construct() {
            parent::Model();
            $this->load->library('Tree', array('table' => 'tree'));
            $this->curid = ($this->uri->segment(3) == FALSE) ? 1 : $this->uri->segment(3);
            $this->TYPES = $this->db->get('types')->result_array();
        }

        function getTree($id, $fields = array('id', 'title', 'parent_id', 'type_id', 'is_default')) {
            static $ret = '';
            $node = $this->tree->getNode($id, $fields);
            if($node != FALSE) {
                if($node['id'] == $this->curid || $node['is_default'] == 1) {
                    $ret .= '<ul '.self::SHOW.'>';
                } else {
                    $ret .= '<ul '.self::HIDE.'>';
                }
                //$ret .= '<li>'.anchor($this->getType($node).'/view/'.$node['id'], $node['title']);

                $childs = $this->tree->getChildNodes($id, $fields);
                if($childs != FALSE) {
                    $ret .= '<ul>';
                        foreach($childs as $child) {
                            $ret .= '<li>'.anchor($this->getType($child).'/view/'.$child['id'], $child['title']);
                            $this->getTree($child['id'], $fields);
                        }
                    $ret .= '</ul>';
                }
                $ret .= '</ul>';
            }
            return $ret;
        }

        function getType($node) {
            return $this->TYPES[$node['type_id'] - 1]['typename'];
        }
    }
?>